package com.ggs.rg.ybjb.adminmodule.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ggs.rg.ybjb.adminmodule.mapper.TOrderMapper;
import com.ggs.rg.ybjb.adminmodule.service.TOrderService;
import com.ggs.rg.ybjb.adminmodule.util.OrderUtil;
import com.ggs.rg.ybjb.pojomodule.pojo.TOrder;
import com.ggs.rg.ybjb.pojomodule.pojo.TUser;
import com.ggs.rg.ybjb.pojomodule.util.CommonUtil;
import com.ggs.rg.ybjb.pojomodule.util.StringTools;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cxw
 * @since 2021-11-02
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    TOrderMapper tOrderMapper;

    @Override
    public JSONObject selectOrders(Integer pagesNumber, Integer status) {
        //存储总页数
        long pages = 0;
        //创建分页对象
        Page<TOrder> page = new Page<>(pagesNumber, 12);
        //设置分页不使用count()函数
//        page = page.setOptimizeCountSql(false).setSearchCount(false);
        //创建条件构造器
        QueryWrapper<TOrder> wrapper = new QueryWrapper<TOrder>();
        //向条件构造器加入条件
        wrapper.orderByDesc("create_time");
        //存储需要返回给前端的订单的所有信息
        List<JSONObject> orderList = new ArrayList();
        //接收查询到的所有订单基本信息(里面可能有前端不需要的信息,所以需要进行处理)
        List<TOrder> orders = new ArrayList<TOrder>();
        if (status == null) {
            Page<TOrder> result = tOrderMapper.selectPage(page, wrapper);
            pages = page.getPages();
            orders = result.getRecords();
            for (TOrder tOrder : orders) {
                if (tOrder.getStatus() == 1){
                    TOrder order = tOrderMapper.selectOrderByOid(Long.valueOf(tOrder.getOid()));
                    JSONObject orderForJsonJson = OrderUtil.orderForJson(order, 1);
                    orderList.add(orderForJsonJson);
                }else {
                    TOrder order = tOrderMapper.selectUnpaidOrderByOid(Long.valueOf(tOrder.getOid()));
                    JSONObject orderForJsonJson = OrderUtil.orderForJson(order, 0);
                    orderList.add(orderForJsonJson);
                }
            }
            JSONObject resultJson = new JSONObject();
            resultJson.put("pagesAmount", pages);
            resultJson.put("pagesNumber", pagesNumber);
            resultJson.put("allOrderMessage", orderList);
            return resultJson;
        } else if (status == 1) {
            IPage<TOrder> result = tOrderMapper.selectOrders(page, wrapper, status);
            pages = result.getPages();
            orders = result.getRecords();
        } else {
            IPage<TOrder> result = tOrderMapper.selectUnpaidOrders(page, wrapper, status);
            pages = result.getPages();
            orders = result.getRecords();
        }
        for (TOrder tOrder : orders) {
            JSONObject result = OrderUtil.orderForJson(tOrder, tOrder.getStatus());
            //将封装好的完整订单信息放入list中,循环
            orderList.add(result);
        }
        JSONObject resultJson = new JSONObject();
        resultJson.put("pagesAmount", pages);
        resultJson.put("pagesNumber", pagesNumber);
        resultJson.put("allOrderMessage", orderList);
        return resultJson;
    }

    //根据oid获取订单,搜索功能
    @Override
    public JSONObject selectTOrderById(Long oid) {
        TOrder order = tOrderMapper.selectById(oid);
        if (StringTools.isNullOrEmpty(order))
            return CommonUtil.sqlErrorJson("查询不到指定订单");
        JSONObject result;
        Integer status = order.getStatus();
        if (status == 1){
            TOrder tOrder = tOrderMapper.selectOrderByOid(oid);
            result = OrderUtil.orderForJson(tOrder,1);
        }else {
            TOrder tOrder = tOrderMapper.selectUnpaidOrderByOid(oid);
            result = OrderUtil.orderForJson(tOrder,0);
        }
        return result;
    }

    //根据oid获取订单详情
    @Override
    public JSONObject selectTOrderDetailById(Long oid) {
        TOrder tOrder = tOrderMapper.selectOrderByOid(oid);
        if (tOrder == null)
            return CommonUtil.sqlErrorJson("查询不到指定订单");
        JSONObject result = OrderUtil.orderForJson(tOrder, tOrderMapper);
        return result;
    }

    @Override
    public JSONObject selectUnpaidTOrderDetailById(Long oid) {
        TOrder tOrder = tOrderMapper.selectUnpaidOrderByOid(oid);
        if (tOrder == null)
            return CommonUtil.sqlErrorJson("查询不到指定订单");
        JSONObject result = OrderUtil.unpaidOrderForJson(tOrder, tOrderMapper);
        return result;
    }

    @Override
    public List<TOrder> selectTOrderByPhone(String phone) {
        return tOrderMapper.selectTOrderByPhone(phone);
    }


}
